<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="结算单号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入结算单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算单名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入结算单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织机构系统编号" prop="organizationId">
        <el-input
          v-model="queryParams.organizationId"
          placeholder="请输入组织机构系统编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商系统编号" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入供应商系统编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同系统编号" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          placeholder="请输入合同系统编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算时间" prop="settlementDate">
        <el-date-picker clearable
          v-model="queryParams.settlementDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结算时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结算金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入结算金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他费用金额" prop="otherAmount">
        <el-input
          v-model="queryParams.otherAmount"
          placeholder="请输入其他费用金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入结算总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算备注" prop="comment">
        <el-input
          v-model="queryParams.comment"
          placeholder="请输入结算备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批流实例id" prop="auditFlowInstanceId">
        <el-input
          v-model="queryParams.auditFlowInstanceId"
          placeholder="请输入审批流实例id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核通过时间" prop="auditPassDate">
        <el-date-picker clearable
          v-model="queryParams.auditPassDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核通过时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="父结算单系统编号" prop="parentSettlementId">
        <el-input
          v-model="queryParams.parentSettlementId"
          placeholder="请输入父结算单系统编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作废原因" prop="invalidReason">
        <el-input
          v-model="queryParams.invalidReason"
          placeholder="请输入作废原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否被锁定，锁定之后不能操作" prop="isLocked">
        <el-input
          v-model="queryParams.isLocked"
          placeholder="请输入是否被锁定，锁定之后不能操作"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单到期日" prop="billDueDate">
        <el-date-picker clearable
          v-model="queryParams.billDueDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择账单到期日">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:finance:settlement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:finance:settlement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:finance:settlement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:finance:settlement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settlementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统编号" align="center" prop="settlementId" />
      <el-table-column label="结算单号" align="center" prop="code" />
      <el-table-column label="结算单名称" align="center" prop="name" />
      <el-table-column label="组织机构系统编号" align="center" prop="organizationId" />
      <el-table-column label="供应商系统编号" align="center" prop="supplierId" />
      <el-table-column label="合同系统编号" align="center" prop="contractId" />
      <el-table-column label="结算时间" align="center" prop="settlementDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算金额" align="center" prop="amount" />
      <el-table-column label="其他费用金额" align="center" prop="otherAmount" />
      <el-table-column label="结算总金额" align="center" prop="totalAmount" />
      <el-table-column label="结算备注" align="center" prop="comment" />
      <el-table-column label="审批类型 0=结算审批 1=作废审批" align="center" prop="auditType" />
      <el-table-column label="审批状态 0=不需要审批 1=审批中 2=审批未通过 3=审批通过 4=审批撤销" align="center" prop="auditStatus" />
      <el-table-column label="审批流实例id" align="center" prop="auditFlowInstanceId" />
      <el-table-column label="审批实例类型，1-严选审批 2-物资工匠审批 3-云枢审批" align="center" prop="auditFlowInstanceType" />
      <el-table-column label="审核通过时间" align="center" prop="auditPassDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditPassDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算类型：1-采购 2-商城 3-混凝土 4-传统租赁 5-专项 6-车辆 7-费用 8-商城租赁 9-货运结算" align="center" prop="settlementType" />
      <el-table-column label="结算状态 10=编制中 20=待确认 30=已拒绝 40=已确认 50=已结算 -100=已作废" align="center" prop="status" />
      <el-table-column label="父结算单系统编号" align="center" prop="parentSettlementId" />
      <el-table-column label="作废原因" align="center" prop="invalidReason" />
      <el-table-column label="提示" align="center" prop="notice" />
      <el-table-column label="是否被锁定，锁定之后不能操作" align="center" prop="isLocked" />
      <el-table-column label="账单到期日" align="center" prop="billDueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billDueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据状态0-无效 1-有效 -999-删除" align="center" prop="commonStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:finance:settlement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:finance:settlement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="结算单号" prop="code">
          <el-input v-model="form.code" placeholder="请输入结算单号" />
        </el-form-item>
        <el-form-item label="结算单名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入结算单名称" />
        </el-form-item>
        <el-form-item label="组织机构系统编号" prop="organizationId">
          <el-input v-model="form.organizationId" placeholder="请输入组织机构系统编号" />
        </el-form-item>
        <el-form-item label="供应商系统编号" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商系统编号" />
        </el-form-item>
        <el-form-item label="合同系统编号" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同系统编号" />
        </el-form-item>
        <el-form-item label="结算时间" prop="settlementDate">
          <el-date-picker clearable
            v-model="form.settlementDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结算时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入结算金额" />
        </el-form-item>
        <el-form-item label="其他费用金额" prop="otherAmount">
          <el-input v-model="form.otherAmount" placeholder="请输入其他费用金额" />
        </el-form-item>
        <el-form-item label="结算总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入结算总金额" />
        </el-form-item>
        <el-form-item label="结算备注" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入结算备注" />
        </el-form-item>
        <el-form-item label="审批流实例id" prop="auditFlowInstanceId">
          <el-input v-model="form.auditFlowInstanceId" placeholder="请输入审批流实例id" />
        </el-form-item>
        <el-form-item label="审核通过时间" prop="auditPassDate">
          <el-date-picker clearable
            v-model="form.auditPassDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核通过时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="父结算单系统编号" prop="parentSettlementId">
          <el-input v-model="form.parentSettlementId" placeholder="请输入父结算单系统编号" />
        </el-form-item>
        <el-form-item label="作废原因" prop="invalidReason">
          <el-input v-model="form.invalidReason" placeholder="请输入作废原因" />
        </el-form-item>
        <el-form-item label="提示" prop="notice">
          <el-input v-model="form.notice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否被锁定，锁定之后不能操作" prop="isLocked">
          <el-input v-model="form.isLocked" placeholder="请输入是否被锁定，锁定之后不能操作" />
        </el-form-item>
        <el-form-item label="账单到期日" prop="billDueDate">
          <el-date-picker clearable
            v-model="form.billDueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择账单到期日">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSettlement, getSettlement, delSettlement, addSettlement, updateSettlement } from "@/api/business/finance/settlement"

export default {
  name: "Settlement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算单表格数据
      settlementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        organizationId: null,
        supplierId: null,
        contractId: null,
        settlementDate: null,
        amount: null,
        otherAmount: null,
        totalAmount: null,
        comment: null,
        auditType: null,
        auditStatus: null,
        auditFlowInstanceId: null,
        auditFlowInstanceType: null,
        auditPassDate: null,
        settlementType: null,
        status: null,
        parentSettlementId: null,
        invalidReason: null,
        notice: null,
        isLocked: null,
        billDueDate: null,
        commonStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "结算单号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "结算单名称不能为空", trigger: "blur" }
        ],
        organizationId: [
          { required: true, message: "组织机构系统编号不能为空", trigger: "blur" }
        ],
        supplierId: [
          { required: true, message: "供应商系统编号不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "结算金额不能为空", trigger: "blur" }
        ],
        otherAmount: [
          { required: true, message: "其他费用金额不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "结算状态 10=编制中 20=待确认 30=已拒绝 40=已确认 50=已结算 -100=已作废不能为空", trigger: "change" }
        ],
        commonStatus: [
          { required: true, message: "数据状态0-无效 1-有效 -999-删除不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询结算单列表 */
    getList() {
      this.loading = true
      listSettlement(this.queryParams).then(response => {
        this.settlementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        settlementId: null,
        code: null,
        name: null,
        organizationId: null,
        supplierId: null,
        contractId: null,
        settlementDate: null,
        amount: null,
        otherAmount: null,
        totalAmount: null,
        comment: null,
        auditType: null,
        auditStatus: null,
        auditFlowInstanceId: null,
        auditFlowInstanceType: null,
        auditPassDate: null,
        settlementType: null,
        status: null,
        parentSettlementId: null,
        invalidReason: null,
        notice: null,
        isLocked: null,
        billDueDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        commonStatus: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.settlementId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加结算单"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const settlementId = row.settlementId || this.ids
      getSettlement(settlementId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改结算单"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.settlementId != null) {
            updateSettlement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSettlement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const settlementIds = row.settlementId || this.ids
      this.$modal.confirm('是否确认删除结算单编号为"' + settlementIds + '"的数据项？').then(function() {
        return delSettlement(settlementIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/finance/settlement/export', {
        ...this.queryParams
      }, `settlement_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
